package com.yc.starter.vcode;

import com.yc.starter.vcode.config.VcodeProperties;
import com.yc.starter.vcode.web.VcodeInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * @author navy
 * @company 源辰信息
 * @date 2024/9/10
 * @email haijunzhou@hnit.edu.cn
 */
@Configuration
@ConditionalOnClass(DispatcherServlet.class)
@ConditionalOnProperty(prefix = "vcode", name = "enable", havingValue="true")
@ComponentScan("com.yc.starter.vcode")
@Slf4j
public class VcodeConfig implements WebMvcConfigurer {

    @Autowired
    VcodeProperties properties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        VcodeInterceptor interceptor = new VcodeInterceptor();
        InterceptorRegistration registration = registry.addInterceptor(interceptor);
        for (String url : properties.getUrls()) {
            registration.addPathPatterns(url);
        }
    }
}
